using Prism.Mvvm;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace JGSY.CourseManagement.Presentation.WpfApp.ViewModels
{
    /// <summary>
    /// 所有ViewModel的基类
    /// </summary>
    public class NotifyPropertyChangedVM : BindableBase
    {
        private bool _isBusy;
        private string _title;

        /// <summary>
        /// 是否忙碌状态
        /// </summary>
        public bool IsBusy
        {
            get => _isBusy;
            set => SetProperty(ref _isBusy, value);
        }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }

        /// <summary>
        /// 安全执行异步操作，自动管理IsBusy状态并处理异常
        /// </summary>
        /// <param name="operation">要执行的异步操作委托</param>
        /// <param name="onException">异常处理委托（可选），未提供时使用全局异常处理</param>
        /// <returns>表示异步操作的任务</returns>
        protected async Task ExecuteSafeAsync(Func<Task> operation, Action<Exception> onException = null)
        {
            IsBusy = true;
            try
            {
                await operation();
            }
            catch (Exception ex)
            {
                onException?.Invoke(ex);
                // 可以在这里添加全局异常处理逻辑
            }
            finally
            {
                IsBusy = false;
            }
        }
    }
}